/*
 * Copyright (c) 2016. 恒昌互联网运营中心.
 */
package com.credithc.eagleeye.console.web;
import com.credithc.eagleeye.console.service.SettingService;
import com.credithc.eagleeye.console.vo.PlatformVo;
import com.credithc.eagleeye.console.vo.ResultVo;
import com.credithc.eagleeye.console.vo.SettingVo;
import com.credithc.eagleeye.console.vo.UserVo;
import com.credithc.eagleeye.util.FastJsonConvert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
 * <br>
 * 类 名: StatController <br>
 * 描 述: 统计 <br>
 * 作 者: zj <br>
 * 创 建： 2018年09月27日 <br>
 * 版 本：v1.0.0 <br>
 * <br>
 * 历 史: (版本) 作者 时间 注释
 */
@Controller
@RequestMapping("/setting")
public class SettingController extends AbstractAction {
	private static final Logger logger = LoggerFactory.getLogger(SettingController.class);

	@Autowired
	private SettingService settingService;

	@Override
	protected String getFlag() {
		return "setting_flag";
	}

	@Override
	protected String getTitle() {
		return "鹰眼配置";
	}

	@RequestMapping(value = "main", method = RequestMethod.GET)
	public String main(HttpServletRequest request, ModelMap map) {
		putPublicAttribute(map);
		return "setting/settingMain";
	}

	@RequestMapping(value = "loadSetting", method = {RequestMethod.GET})
	@ResponseBody
	public ResultVo<String> loadSetting( ModelMap map) {
		ResultVo<String> resultVo=new ResultVo();
		try {
			SettingVo settingVo = this.settingService.loadSetting();
			if (settingVo == null) {
				settingVo = new SettingVo();
				settingVo.setUserInfoTypeList(new ArrayList<String>());
			}
			resultVo.setCode("1");
			resultVo.setMessage("成功");
			resultVo.setData(FastJsonConvert.convertObjectToJsonWithPrettyFormat(settingVo));
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}
		return resultVo;
	}

	@RequestMapping(value = "saveSetting", method = {RequestMethod.POST})
	@ResponseBody
	public ResultVo saveSetting(String settingJson, HttpServletRequest request, ModelMap map) {
		ResultVo resultVo=new ResultVo();
		try {
			SettingVo settingVo = FastJsonConvert.convertJSONToObject(settingJson, SettingVo.class);
			if(settingVo!=null) {
				settingService.saveSetting(settingVo);
			}
			resultVo.setCode("1");
			resultVo.setMessage("成功");
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}
		return resultVo;
	}

	@RequestMapping(value = "loadPlatformList", method = {RequestMethod.GET})
	@ResponseBody
	public ResultVo<String> loadPlatformList( ModelMap map) {
		ResultVo<String> resultVo=new ResultVo();
		try {
			List<PlatformVo> platformVoList = this.settingService.loadAllPlatform();
			if (platformVoList == null || platformVoList.size() == 0) {
				platformVoList = new ArrayList<>();
				platformVoList.add(new PlatformVo());
			}
			resultVo.setCode("1");
			resultVo.setMessage("成功");
			resultVo.setData(FastJsonConvert.convertObjectToJsonWithPrettyFormat(platformVoList));
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}

		return resultVo;
	}

	@RequestMapping(value = "savePlatformList", method = {RequestMethod.POST})
	@ResponseBody
	public ResultVo savePlatformList(String platformListJson, HttpServletRequest request, ModelMap map) {
		ResultVo resultVo=new ResultVo();
		try {
			List<PlatformVo> platformVoList = FastJsonConvert.convertJSONToArray(platformListJson, PlatformVo.class);
			settingService.savePlatformList(platformVoList);
			resultVo.setCode("1");
			resultVo.setMessage("成功");
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}

		return resultVo;
	}

	@RequestMapping(value = "loadUserList", method = {RequestMethod.GET})
	@ResponseBody
	public ResultVo<String> loadUserList( ModelMap map) {
		ResultVo<String> resultVo=new ResultVo();
		try {
			List<UserVo> userVoList = this.settingService.loadAllUser();
			if (userVoList == null || userVoList.size() == 0) {
				userVoList = new ArrayList<>();
				userVoList.add(new UserVo());
			}
			resultVo.setCode("1");
			resultVo.setMessage("成功");
			resultVo.setData(FastJsonConvert.convertObjectToJsonWithPrettyFormat(userVoList));
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}
		return resultVo;
	}

	@RequestMapping(value = "saveUserList", method = {RequestMethod.POST})
	@ResponseBody
	public ResultVo saveUserList(String userListJson, HttpServletRequest request, ModelMap map) {
		ResultVo resultVo=new ResultVo();
		try {
			List<UserVo> userVoList = FastJsonConvert.convertJSONToArray(userListJson, UserVo.class);
			settingService.saveUserList(userVoList);
			resultVo.setCode("1");
			resultVo.setMessage("成功");
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setCode("0");
			resultVo.setMessage(e.getMessage());
		}
		return resultVo;
	}

}
